package com.ruoyi.pay.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pay.domain.PayConfig;
import com.ruoyi.pay.service.IPayConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 支付宝支付配置Controller
 *
 * @author ruoyi
 * @date 2024-12-02
 */
@RestController
@RequestMapping("/system/payConfig")
public class PayConfigController extends BaseController {
    @Autowired
    private IPayConfigService payConfigService;

/**
 * 查询支付配置列表
 */
@PreAuthorize("@ss.hasPermi('system:config:list')")
@GetMapping("/list")
    public TableDataInfo list(PayConfig payConfig) {
        startPage();
        List<PayConfig> list = payConfigService.selectPayConfigList(payConfig);
        return getDataTable(list);
    }

    /**
     * 导出支付配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @Log(title = "支付配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PayConfig payConfig) {
        List<PayConfig> list = payConfigService.selectPayConfigList(payConfig);
        ExcelUtil<PayConfig> util = new ExcelUtil<PayConfig>(PayConfig. class);
        util.exportExcel(response, list, "支付配置数据");
    }

    /**
     * 获取支付配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(payConfigService.selectPayConfigById(id));
    }

    /**
     * 新增支付配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "支付配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PayConfig payConfig) {
        AjaxResult result = AjaxResult.success();
        HashMap<String, String> map  = payConfigService.insertPayConfig(payConfig);
        if(!"200".equals(map.get("code"))){
            return AjaxResult.error(map.get("msg"));
        }
        return result;
    }

    /**
     * 修改支付配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "支付配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PayConfig payConfig) {
        return toAjax(payConfigService.updatePayConfig(payConfig));
    }

    /**
     * 删除支付配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "支付配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(payConfigService.deletePayConfigByIds(ids));
    }
}
